<?php

namespace app\api\command;
use app\common\model\Packages;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Tongbu extends Command
{
    protected $model = null;

    protected function configure()
    {
        $this->setName('tongbu')->setHelp('同步数据')->setDescription('同步数据');

    }

    protected function execute(Input $input, Output $output)
    {

        $data['appid']     = config('site.ooen_appid');
        $data['timestamp'] = time();
        $data['nonce_str'] = Random::alnum(32);
        $string = ascll($data);
        $stringSignTemp  = $string.'&appsecret='.config('site.open_appsecret');
        $data['sign']    = mb_strtoupper( md5($stringSignTemp));
        $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/packages";//获取套餐
        $res = httpPost($url,$data);
        $arr = json_decode($res,true);
        if(!empty($arr['list'])){
            $r1 = '';
            $model = new Packages();
            foreach ($arr['list'] as $k=>$v){
                $parasm['packages_id'] = $v['id'];
//                $parasm['name']        = $v['name'];
                if($v['opText'] == "电信"){
                    $parasm['cate_id'] = 1;
                }elseif ($v['opText'] == "联通"){
                    $parasm['cate_id'] = 2;
                }elseif ($v['opText'] == "移动"){
                    $parasm['cate_id'] = 3;
                }
                $parasm['photos']      = $v['photos'][0]??'';
                if(!empty($v['theme_config']['photoLists'])){
                    $parasm['photoLists']  = implode(',', $v['theme_config']['photoLists']);
                }
                $parasm['h5url']              = $v['h5Url'];
                $parasm['Inception_rebate']    = $v['rebate'];
                $parasm['data_json']  = json_encode($v,true);
//                if(!empty($v['subs'][0]['status'])){
//                    $parasm['status']     = $v['subs'][0]['status'];
//                }else{
//                    $parasm['status']     = 0;
//                }
                $info = $model->where(['packages_id'=>$v['id']])->find();
                if($info){
                    unset($parasm['opText'],$parasm['cate_id']);
                    $r = $info->allowField(true)->save($parasm);
                }else{
                    $r1 = $model->create($parasm);
                }
            }
            if($r){
                echo "更新成功";
            }
            if($r1){
                echo '添加成功';
            }
        }

    }



}
